package com.example.demo.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.example.demo.entity.Product;
import com.example.demo.mapper.ProductMapper;
import com.example.demo.service.ProductService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.demo.util.SolrUtil;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiah
 * @since 2018-05-29
 */
@Service

public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
       @Autowired
       private SolrClient solrClient;

       @Override
       public SolrDocumentList getProduct(String search) throws Exception {
              SolrQuery solrQuery = new SolrQuery(search);
              solrQuery.setHighlight(true);//开启高亮
              solrQuery.addHighlightField("name");
              //设置前缀
              solrQuery.setHighlightSimplePre("<span style='color:red'>");
              //设置后缀
              solrQuery.setHighlightSimplePost("</span>");
              /*设置分段的段数  默认是1*/
              //solrQuery.setHighlightSnippets(2);
               /*设置分段的段长 默认是100*/
              solrQuery.setHighlightFragsize(200);
              QueryResponse queryResponse = solrClient.query(solrQuery);
              System.out.println(queryResponse);
              String ss = queryResponse.getRequestUrl();
              Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
              SolrDocumentList solrDocumentList = queryResponse.getResults();
              List li = new ArrayList();
              solrDocumentList.stream().forEach(s -> {
                     List oo = ( List ) map.get(s.get("id")).get("name");
                     System.out.println(oo.get(0));
                     s.put("name", oo.get(0));

              });
              return solrDocumentList;
       }

       @Override
       public Object addProduct() throws Exception {
              /*SolrInputDocument solrInputFields = new SolrInputDocument();
              solrInputFields.addField("id", "1");
              solrInputFields.addField("name", "我是谁在哪里333");
              solrInputFields.addField("content", "一生襟抱未曾开");
              solrClient.add(solrInputFields);*/
              List list = this.selectList(new EntityWrapper<>());
              solrClient.addBeans(list);
              UpdateResponse updateResponse=solrClient.commit();
              return "ok";
       }
}
